//
//  YQOverseasBuyersCtlViewController.m
//  OverseasImmigration
//
//  Created by yuqin on 2019/5/24.
//  Copyright © 2019年 yuqin. All rights reserved.
//

#import "YQOverseasBuyersCtlViewController.h"
#import "YQHousCell.h"
#import "YQOverseasModel.h"
#import "SQFiltrateView.h"
#import "YQOverseasDetailCtl.h"

@interface YQOverseasBuyersCtlViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>

@property (nonatomic, strong) UITableView *houseTableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) SQFiltrateItem *areaView;
@property (nonatomic, strong) SQFiltrateItem *totalView;
@property (nonatomic, strong) SQFiltrateItem *typeView;
@property (nonatomic, strong) SQFiltrateItem *screeningView;
//@property (nonatomic, strong) NSMutableSet *selectSet;
/**
 区域
 */
@property (nonatomic, strong) NSMutableArray *areaArr;

/**
 总价
 */
@property (nonatomic, strong) NSMutableArray *totalPriceArr;

/**
 类型
 */
@property (nonatomic, strong) NSMutableArray *typeArr;

/**
 筛选
 */
@property (nonatomic, strong) NSMutableArray *screeningArr;

@property (nonatomic, copy) NSString *chooseIndex;//点击头部的那个
@property (nonatomic, copy) NSString *chooseContext;//选择的内容

@end

@implementation YQOverseasBuyersCtlViewController
{
    NSInteger _page;
    NSInteger _limit;
    SQFiltrateView *filtrateView;
    __block NSInteger _selectBtnIndex;
}
- (NSMutableArray *)areaArr {
    if (!_areaArr) {
        _areaArr = [NSMutableArray array];
    }
    return _areaArr;
}

- (NSMutableArray *)totalPriceArr {
    if (!_totalPriceArr) {
        _totalPriceArr = [NSMutableArray array];
    }
    return _totalPriceArr;
}

- (NSMutableArray *)typeArr {
    if (!_typeArr) {
        _typeArr = [NSMutableArray array];
    }
    return _typeArr;
}

- (NSMutableArray *)screeningArr {
    if (!_screeningArr) {
        _screeningArr = [NSMutableArray array];
    }
    return _screeningArr;
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
    self.title = @"海外置业";
    [self setupTableView];
    _page = 1;
    _limit = 10;
    
    //获取默认数据
    [self loadNaormalDataWithIndexWithCountry:@"500" type:@"公寓" price:@"20-50万" area:@"50-100平"];
    [self loadScreeningData];
    
}

- (void)setupChooseHeaderWithAreaArr:(NSMutableArray *)areaArr totalPriceArr:(NSMutableArray *)totalArr typeArr:(NSMutableArray *)typeArr screeningArr:(NSMutableArray *)screeningArr {
    NSArray *titleArr = @[@"区域",@"总价",@"项目",@"筛选"];
    for (NSInteger index = 0; index < titleArr.count; index++) {
        SQFiltrateItem *item2 = [[SQFiltrateItem alloc]init];
        item2.listType = OptionListType_Cell;
        item2.numberType = OptionNumberType_Single;
        item2.title = titleArr[index];
        if (index == 0) {
            self.areaView = item2;
            item2.optionData = [areaArr copy];
        }else if (index == 1) {
            self.totalView = item2;
            item2.optionData = [totalArr copy];
        }else if (index == 2) {
            self.typeView = item2;
            item2.optionData = [typeArr copy];
        }else {
            self.screeningView = item2;
            item2.optionData = [screeningArr copy];
        }
        
    }
    filtrateView = [[SQFiltrateView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height+[UIApplication sharedApplication].statusBarFrame.size.height, self.view.frame.size.width, 40)
                                          filtrateItems:@[self.areaView,self.totalView,self.typeView,self.screeningView]];
    [self.view addSubview:filtrateView];
    @weakify(self);
    filtrateView.Itemlock = ^(NSInteger index) {
        NSLog(@"%ld",index);
        @strongify(self);
        self->_selectBtnIndex = index;
    };
    
    [filtrateView touchBlock:^(SQFiltrateView * _Nonnull view, SQFiltrateItem * _Nonnull item) {
        @strongify(self);
        NSLog(@"%@:%@",item.title,item.choseSet);
        NSArray *selectArr = [item.choseSet allObjects];
        NSInteger element = [[selectArr lastObject] integerValue];
        if (self->_selectBtnIndex == 0) {
            NSString *str = [areaArr objectAtIndex:element];
            NSArray *titleArr = [str componentsSeparatedByString:@":"];
            [self loadNaormalDataWithIndexWithCountry:[titleArr lastObject] type:@"公寓" price:@"20-50万" area:@"50-100平"];
//
        }else if (self->_selectBtnIndex == 1) {
            NSString *str = [totalArr objectAtIndex:element];
            [self loadNaormalDataWithIndexWithCountry:@"500" type:str price:@"20-50万" area:@"50-100平"];
        }else if (self->_selectBtnIndex == 2) {
            NSString *str = [typeArr objectAtIndex:element];
            [self loadNaormalDataWithIndexWithCountry:@"500" type:@"公寓" price:str area:@"50-100平"];
        }else {
            NSString *str = [screeningArr objectAtIndex:element];
            [self loadNaormalDataWithIndexWithCountry:@"500" type:@"公寓" price:@"20-50万" area:str];
        }
        
    }];
}

- (void)setupTableView {
    self.houseTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 104, SCREENWIDTH, SCREENHEIGHT-104) style:UITableViewStylePlain];
    self.houseTableView.delegate = self;
    self.houseTableView.dataSource = self;
    self.houseTableView.tableFooterView = [[UIView alloc] init];
    YQ_EmptyView(self.houseTableView);
    [self.houseTableView registerNib:[UINib nibWithNibName:@"YQHousCell" bundle:nil] forCellReuseIdentifier:@"YQHousCell"];
    [self.view addSubview:self.houseTableView];
}

#pragma mark - 获取列表接口
- (void)loadNaormalDataWithIndexWithCountry:(NSString *)country
                                       type:(NSString *)type
                                      price:(NSString *)price
                                       area:(NSString *)are {
    NSString *urlStr = [NSString stringWithFormat:@"%@user/houseList",BaseHttpUrl];
    NSDictionary *dict = @{@"page":StringFromLongInt(_page),
                           @"limit":StringFromLongInt(_limit),
                           @"country":country,
                           @"type":type,
                           @"price":price,
                           @"area":are
                           };
    [[HTTPRequstManger sharedHTTPRequstManger] getRequestWithUrlString:urlStr appendParameters:dict successBlock:^(id responseObject) {
        NSArray *array = responseObject[@"data"][@"dataList"];
        for (NSDictionary *dict in array) {
            
//            YQOverseasModel *model = [YQOverseasModel mj_objectWithKeyValues:dict];
//            [self.dataArr addObject:model];
            YQOverseasModel *model = [[YQOverseasModel YQOverseasModelshared] mj_setKeyValues:dict];
            [self.dataArr addObject:model];
        }
        [self.houseTableView reloadData];
    } failureBlock:^(NSError *error) {
        
    }];
}

#pragma mark - 获取筛选列表数据
- (void)loadScreeningData {
    NSString *urlStr = [NSString stringWithFormat:@"%@user/houseSearch",BaseHttpUrl];
    [[HTTPRequstManger sharedHTTPRequstManger] getRequestWithUrlString:urlStr appendParameters:nil successBlock:^(id responseObject) {
        NSDictionary *dict = responseObject[@"data"];
        for (NSString *keys in [dict allKeys]) {
            if ([keys isEqualToString:@"country"]) {
                self.areaArr = [[dict objectForKey:keys] mutableCopy];
            }else if ([keys isEqualToString:@"price"]) {
                self.totalPriceArr = [[dict objectForKey:keys] mutableCopy];
            }else if ([keys isEqualToString:@"houseType"]) {
                self.typeArr = [[dict objectForKey:keys] mutableCopy];
            }else if ([keys isEqualToString:@"wuye"]) {
                self.screeningArr = [[dict objectForKey:keys] mutableCopy];
            }
        }
        [self setupChooseHeaderWithAreaArr:self.areaArr totalPriceArr:self.totalPriceArr typeArr:self.typeArr screeningArr:self.screeningArr];
        [self.houseTableView reloadData];
        //设置tableView的头部

    } failureBlock:^(NSError *error) {
        
    }];
}

YQ_Empty_Deletage_DataSources(@"暂无数据")

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return FIT(140);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YQHousCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YQHousCell" forIndexPath:indexPath];
    YQOverseasModel *model = self.dataArr[indexPath.row];
    [cell loadHouseCellWithModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    YQOverseasModel *model = self.dataArr[indexPath.row];
    YQOverseasDetailCtl *ctl = [[YQOverseasDetailCtl alloc] init];
    ctl.aid = model.aid;
    YQ_PUSH(ctl);
}


@end
